
/**
 *tagfolder
 *Licensed under LGPL
 *http://code.google.com/p/tagfolder
 *chuanshenglu@gmail.com
 *xrjieyee@gmail.com
 *
 */
#ifndef __TFCHANNEL_H__
#define __TFCHANNEL_H__

#include "TFstd.h"
#include "TFString.h"

/**
 *Channel class abstracts the communication channel of processes;
 */
class TFChannel
{
    protected:
        TFString ConnectionString;	//ConnectionString,represent the Channel, either NamedPipe name,SharedMem Addr,etc.
    public:
        TFChannel()
            :ConnectionString()
        {
        }
        TFChannel(TFString& str)
            :ConnectionString( str)
        {

        }
        TFChannel(TFChannel&)
        {
        }
        virtual ~TFChannel()
        {
        }
        virtual bool SetConnectionString(TFString&);
        virtual TFString& GetConnectionString();
    public:
};

/**
 *A duplex channel is a channel that can be both read and write;
 *either operation will block the caller thread/process 
 */
class TFDuplexChannel :public TFChannel
{
    public:
        TFDuplexChannel()
        {
        }
        TFDuplexChannel(TFString& str)
        {
        }
        TFDuplexChannel(TFDuplexChannel&)
        {
        }
        virtual ~TFDuplexChannel()
        {
        }
    public:
        /*
         *Param: Data ptr,size to be Write in byte;
         *Return: if succeeded
         */
        virtual bool Put(void *, unsigned long)=0;
        /*
         *Param:Data ptr to receive, size to be read in byte;
         *Return: Bytes acctually read;
         */
        virtual unsigned long Get(void *, unsigned long )=0;

        //Various read function for conveniont;
        virtual char GetChar()=0;
        virtual std::string GetString()=0;
        virtual bool Put(std::string&)=0;
        //Various Write function for conv
        //template<typename DataType>
        //bool Put(DataType& data)
        //{
        //	return Put( (void*) &data , sizeof(data) );
        //}
        //template<typename DataType>
        //bool Put(DataType* ptr,unsigned int count)
        //{
        //	return Put ( (void*)ptr , count*( sizeof( DataType ) ) );
        //}
};

//=====================================================
//Struct of fix-length Message, for control purpose;
//=====================================================
//Do not inherit from this!!!
class TFMessage
{
    public:
        TFMessage();
        TFMessage(TFMessage&);
        virtual ~TFMessage();
    public:
        unsigned long ID;
        char Signal[8];
        unsigned long Sendor;
        unsigned long Receiver;
    public:
        unsigned long NextSize;
        unsigned long NextSize2;
        unsigned long NextSize3;
    private:
};

#define TFMSG_ADDTAG				1	
#define TFMSG_ADDFILE			2
#define TFMSG_LINKTAGTOFILE		3
#define TFMSG_GETALLTAGS		4
#define TFMSG_GETALLFILES		5
#define TFMSG_GETTAGSOFFILE	6
#define TFMSG_GETFILESWITHTAG	7
#define TFMSG_TERMINATESERVER	8

#endif
